//
//  SLTimerCell.m
//  Objective-C
//
//  Created by S_Line on 2018/9/6.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLTimerCell.h"

#import "WeakProxy.h"

#import "NSTimer+SLCategory.h"

@interface SLTimerCell()

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, weak) CADisplayLink *displayLink;
@property (nonatomic, assign) int64_t onePercentSec;
@property (nonatomic, assign) NSTimeInterval onePercentSecD;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@end

@implementation SLTimerCell

- (void)setTimerModel:(SLTimerModel *)timerModel {
    _timerModel = timerModel;
    
    [self setTimer];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initView];
}

- (void)initView {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setTimer {
    self.onePercentSec = 0;
    self.onePercentSecD = 0.0;
    
    switch (self.timerModel.timerType) {
        case SLTimerTypeTimer: {
            if (self.displayLink) {
                [self.displayLink invalidate];
                self.displayLink = nil;
            }
            
            __weak typeof(self) weakself = self;
            self.timer = [NSTimer sl_timerWithTimeInterval:0.01 userInfo:nil repeats:true block:^(NSTimer *timer) {
                [weakself count];
            }];
            [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        }
            break;
        case SLTimerTypeDisplayLink: {
            if (self.timer) {
                [self.timer invalidate];
                self.timer = nil;
            }
            CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:[WeakProxy proxyWithTarget:self] selector:@selector(displayLinkCount:)];
            
            [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
            self.displayLink = displayLink;
        }
            break;
        case SLTimerTypeGCD: {
            
        }
            break;
    }
}

+ (void)sl_displayLinkWithBlock:(void(^)(CADisplayLink *displayLink))block {
    
}

- (void)count {
    SLog(@"-----------timer----------");
    self.onePercentSec += 1;
    
    int64_t onePercentSecs = self.onePercentSec % 100;
    int64_t seconds = (self.onePercentSec / 100) % 60;
    int64_t minuts = (self.onePercentSec / 100 / 60) % 60;
    int64_t hours = (self.onePercentSec / 100 / 60 / 60) % 24;
    
    NSString *countText = [NSString stringWithFormat:@"%2lld.%2lld.%2lld.%2lld", hours, minuts, seconds, onePercentSecs];
    self.countLabel.text = countText;
}

- (void)displayLinkCount:(CADisplayLink *)displayLink {
    SLog(@"-----------display----------");
    if (!self) {
        [displayLink invalidate];
    }
    static NSTimeInterval countStep = 1.0 / 60.0 * 100.0;
    self.onePercentSecD += countStep;
    self.onePercentSec = [[NSNumber numberWithDouble:self.onePercentSecD] longLongValue];
    
    int64_t onePercentSecs = self.onePercentSec % 100;
    int64_t seconds = (self.onePercentSec / 100) % 60;
    int64_t minuts = (self.onePercentSec / 100 / 60) % 60;
    int64_t hours = (self.onePercentSec / 100 / 60 / 60) % 24;
    
    NSString *countText = [NSString stringWithFormat:@"%2lld.%2lld.%2lld.%2lld", hours, minuts, seconds, onePercentSecs];
    self.countLabel.text = countText;
}

- (void)gcdTimer {
//    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
//    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 10*NSEC_PER_SEC, 1*NSEC_PER_SEC); //每10秒触发timer，误差1秒
//    dispatch_source_set_event_handler(timer, ^{
//        // 定时器触发时执行的 block
//    });
//    dispatch_resume(timer);
//    // 了解GCD倒计时的原理，需要我们最好阅读一下libdispatch源码。当然，如果你不想阅读，直接往下看也可以。
//    dispatch_source_create这个API为一个dispatch_source_t类型的结构体ds做了分配内存和初始化操作，然后将其返回。
//    
//    // 下面从底层源码的角度来研究这几行代码的作用。首先是 dispatch_source_create 函数，它和之前见到的 create 函数都差不多，对 dispatchsourcet 对象做了一些初始化工作:
//    
//    dispatch_source_t ds = NULL;
//    ds = _dispatch_alloc(DISPATCH_VTABLE(source), sizeof(struct dispatch_source_s));
//    _dispatch_queue_init((dispatch_queue_t)ds);
//    ds->do_suspend_cnt = DISPATCH_OBJECT_SUSPEND_INTERVAL;
//    ds->do_targetq = &_dispatch_mgr_q;
//    dispatch_set_target_queue(ds, q);
//    return ds;
}

- (void)dealloc {
    SLog(@"-----------cell 释放了----------");
}

@end
